//
//  HGRealNameVC.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/8/11.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGRealNameVC.h"
#import "QQAipOcrManager.h"

@interface HGRealNameVC ()

@property (strong, nonatomic) NSArray *textFieldCofigs;

@property (strong, nonatomic) NSMutableArray<UITextField *> *textFields;

@end

@implementation HGRealNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initSubViews];
    
    [[QQAipOcrManager shareOCR] callBackIdcard:^(NSDictionary *userDict) {
        _textFields.firstObject.text = userDict[@"name"];
        _textFields[1].text = userDict[@"cardNum"];
        _textFields.lastObject.text = userDict[@"address"];
    }];
}

- (void)initSubViews {
    
    self.title = @"实名认证";
    CGFloat margin = 15;
    self.view.backgroundColor = kColorBackground;
    
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headView];
    
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.offset(150);
    }];
    
    NSArray *imgsName = @[@"sfzzm",@"sfzbm"];
    for (int i = 0; i < imgsName.count; i++) {
        UIButton *imgBtn = [[UIButton alloc] init];
        imgBtn.tag = 100 + i;
        imgBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        imgBtn.layer.masksToBounds = YES;
        [imgBtn setImage:[UIImage imageNamed:imgsName[i]] forState:(UIControlStateNormal)];
        [imgBtn addTarget:self action:@selector(taskPhoto:) forControlEvents:(UIControlEventTouchUpInside)];
        [headView addSubview:imgBtn];
    }
    
    [headView.subviews mas_distributeViewsAlongAxis:(MASAxisTypeHorizontal) withFixedSpacing:6 leadSpacing:margin tailSpacing:margin];
    [headView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headView);
        make.size.mas_equalTo(CGSizeMake(170 * kScale  , 105 *kScale));
    }];
    
    UIView *infoView = [[UIView alloc] init];
    infoView.backgroundColor = kColorBackground;
    [self.view addSubview:infoView];
    
    [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headView.mas_bottom).offset(10);
        make.left.right.equalTo(self.view);
        make.height.offset(48 *self.textFieldCofigs.count + 5);
    }];
    
    [self.textFieldCofigs enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor whiteColor];
        [infoView addSubview:bgView];
        
        UILabel *label = [[UILabel alloc] init];
        label.textColor = kColorText;
        label.font = [UIFont systemFontOfSize:14];
        label.text = obj[@"name"];
        [bgView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(margin);
            make.centerY.equalTo(bgView);
        }];
        
        UITextField *textFiele = [[UITextField alloc] init];
        textFiele.placeholder = obj[@"promtor"];
        textFiele.textColor = kColorDefult;
        textFiele.keyboardType = (idx == 1 || idx == 2) ? UIKeyboardTypeDecimalPad : UIKeyboardTypeDefault;
        textFiele.font = [UIFont systemFontOfSize:14];
        [bgView addSubview:textFiele];
        
        [self.textFields addObject:textFiele];
        
        [textFiele mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bgView).offset(85);
            make.right.offset(-margin);
            make.centerY.height.equalTo(bgView);
        }];
    }];
    
    [infoView.subviews mas_distributeViewsAlongAxis:(MASAxisTypeVertical) withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    
    [infoView.subviews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(48);
        make.left.right.equalTo(infoView);
    }];
    
    
    UIButton *sumbit = [[UIButton alloc] init];
    [sumbit setTitle:@"提交认证" forState:(UIControlStateNormal)];
    sumbit.backgroundColor = kColorMainTheme;
    sumbit.layer.cornerRadius = 5;
    [sumbit addTarget:self action:@selector(actionCilk) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:sumbit];
    
    [sumbit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(margin);
        make.bottom.right.offset(-margin);
        make.height.offset(48);
    }];
    
}

#pragma mark - 调用摄像头 0.身份证正面
- (void)taskPhoto:(UIButton *)sender {
    QQAipOcrManagerType type = sender.tag == 100 ? QQAipOcrManagerTypeIdCardFont : QQAipOcrManagerTypeIdCardBank;
    [[QQAipOcrManager shareOCR] begainwithType:type];
    
    [[QQAipOcrManager shareOCR] imgBack:^(UIImage *bankImag) {
        [sender setImage:bankImag forState:(UIControlStateNormal)];
    }];
}

#pragma mark - 实名认证提交
- (void)actionCilk {
    
}

- (NSArray<NSDictionary *> *)textFieldCofigs {
    if (!_textFieldCofigs) {
        _textFieldCofigs = @[@{
                                 @"name" : @"姓名",
                                 @"color" : [UIColor clearColor],
                                 @"promtor" : @"请输入持卡人姓名"
                                 },@{
                                 @"name" : @"证件号码",
                                 @"color" : kColorWithFloat(0xea4436),
                                 @"promtor" : @"请输入身份证号码"
                                 },@{
                                 @"name" : @"手机号码",
                                 @"color" : kColorWithFloat(0xfca805),
                                 @"promtor" : @"请输入手机号码"
                                 },@{
                                 @"name" : @"电子邮箱",
                                 @"color" : kColorWithFloat(0x4286f5),
                                 @"promtor" : @"请输入您的邮箱地址"
                                 },@{
                                 @"name" : @"所在地区",
                                 @"color" : kColorWithFloat(0x4286f5),
                                 @"promtor" : @"请选择您所在地区"
                                 },@{
                                 @"name" : @"详细地址",
                                 @"color" : kColorWithFloat(0x4286f5),
                                 @"promtor" : @""
                                 }];
    }
    return _textFieldCofigs;
}

- (NSMutableArray<UITextField *> *)textFields {
    if (!_textFields) {
        _textFields = [@[] mutableCopy];
    }
    return _textFields;
}

@end
